<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/18
 * Time: 17:24
 */

namespace app\admin\model;


use think\Model;

class ClassroomType extends Model
{
    /**
     * 课程类型列表
     * @param $page
     * @param $limit
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function classroomTypeList($page, $limit){
        $lists = $this->alias('uct')
            ->join('up_admin ua1', 'ua1.id = uct.create_admin', 'left')
            ->join('up_admin ua2', 'ua2.id = uct.update_admin', 'left')
            ->field('classroom_id, classroom_name, uct.create_time, uct.update_time, 
            ua1.real_name as create_name, ua2.real_name as update_name')
            ->page($page, $limit)
            ->order('classroom_id desc')
            ->select();
        return $lists;
    }
    
    /**
     * 获取记录总数，用于分页
     * @param $page
     * @param $limit
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getCount(){
        $count = $this->count();
        return $count;
    }
    
    /**
     * 判断课程类型名称有没有重复
     * @param $classroom_name
     * @return array|false|\PDOStatement|string|Model
     */
    public function sameName($classroom_name){
        $res = $this->where('classroom_name', $classroom_name)->find();
        return $res;
    }
    
    /**
     * 添加课程类型
     * @param $data
     * @return false|int
     */
    public function insertClassroomType($data){
        $res = $this->allowField(true)->save($data);
        return $res;
    }
    
    /**
     * 获取课程类型名称
     * @param $classroom_id
     * @return mixed
     */
    public function getTypeName($classroom_id){
        $classroom_name = $this->where('classroom_id', $classroom_id)->value('classroom_name');
        return $classroom_name;
    }
    
    /**
     * 更新课程分类信息
     * @param $classroom_id
     * @param $data
     * @return false|int
     */
    public function updateClassroomType($classroom_id, $data){
        $res = $this->where('classroom_id', $classroom_id)->update($data);
        return $res;
    }
    
    /**
     * 删除类型
     * @param $classroom_id
     * @return int
     */
    public function delClassroomType($classroom_id){
        $res = $this->where('classroom_id', $classroom_id)->delete();
        return $res;
    }
    
    /**
     * 获取分类名称、ID
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getTypeInfo(){
        $typeLists = $this->field('classroom_id, classroom_name')->select();
        return $typeLists;
    }
}